import {ref } from "vue";
import useInstance from "@/hooks/useInstance";
export const useRoleAddAndEdit = (dialog,onClose,onShow,addRoleModel,emit) => {
  
  const {global} = useInstance()
  const addRoleForm = ref()
  const confirm =()=>{
    addRoleForm.value.validate((valid) => {
      if (valid) {
        // 调用父组件的save方法
        emit('save', addRoleModel)
        onClose()
      }else {
        console.log('error submit!')
        return false
      }
    })
  }
   
  const show = (type,row)=>{
    console.log('----------show------------',row);
    // 修改Dialog的高度
    dialog.height = 300
    dialog.width = 400
    // 根据操作修改dialog标题
    type==='add'?dialog.title = '新增角色' : dialog.title = '编辑角色';
    onShow()
    // 清空表单信息
    Object.keys(addRoleModel).forEach(key => {
      addRoleModel[key] = ''
    })
    if(type ===  'edit'){
      Object.assign(addRoleModel,row)
   }
   addRoleModel.action = type
  }
  return{
    show,
    confirm,
    addRoleForm
  }
}